package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xymarket.adapter.CartAdapter;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.po.ShoppingCart;
import com.example.xymarket.httpUtil.CartService;
import com.example.xymarket.common.MyHandler;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCartActivity extends AppCompatActivity {

    public static List<ShoppingCart> shoppingCarts=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_shopping_cart);
        StatusBar.setStatusBarColor(ShoppingCartActivity.this, StatusBar.yellow);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        getMyCart();

    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                updateListView((List<ShoppingCart>) msg.obj);
            }

            if(msg.what==2) {
                shoppingCarts.add((ShoppingCart) msg.obj);
                updatePrice();

            }

            if(msg.what==-2) {
                shoppingCarts.remove((ShoppingCart) msg.obj);
                updatePrice();
            }

            if(msg.what==100) {
                Intent intent=new Intent(getApplicationContext(),GoodsActivity.class);
                Integer id=(Integer) msg.obj;
                intent.putExtra("id", id);
                startActivity(intent);
            }
        }
    };

    public void Buy(View view)
    {
        Intent intent=new Intent(getApplicationContext(),BuyActivity.class);
        intent.putExtra("price",price);
        startActivity(intent);

    }

    public int price=0;
    @SuppressLint("SetTextI18n")
    public void updatePrice()
    {
        int i=0;
        for(ShoppingCart s:shoppingCarts)
        {
            i+=s.getNum()*s.getGoodsEntity().getPrice();
        }
        TextView total_price=(TextView)findViewById(R.id.total_price);
        price=i;
        total_price.setText(String.valueOf(i/100)+"." +String.valueOf(i%100)+"¥");
    }

    public void getMyCart()
    {
        MyHandler myHandler=new MyHandler(getApplicationContext(),handler);
        CartService cartService=new CartService(myHandler);
        cartService.getMyCart();
    }
    public void updateListView(List<ShoppingCart> list)
    {
        if(list==null)return;
        ListView listView = (ListView) findViewById(R.id.ll1);
        try {
            CartAdapter adapter = new CartAdapter(list,this,R.layout.item_shopping_cart,handler);
            listView.setAdapter(adapter);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }


    /***
     * 页面跳转
     */
    public void toLogin()
    {
        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toGoods(View view)
    {
        Intent intent = new Intent(getApplicationContext(), GoodsActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toHome(View view)
    {
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toClassify(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ClassifyActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toMine(View view)
    {
        Intent intent = new Intent(getApplicationContext(), MineActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }


    /**
     * 再按一次退出程序
     * 用时间来计算两次返回之间的时间
     */
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}